<template>
  <div class="container">
    <div class="form-container">
      <h2 class="text-center">管理员登录</h2>
      <el-divider/>
      <el-form>
        <el-form-item>
          <el-input
            type="text"
            v-model="form.account"
            placeholder="请输入账号..."
          />
        </el-form-item>
        <el-form-item>
          <el-input
            type="password"
            v-model="form.password"
            placeholder="请输入密码..."
          />
        </el-form-item>
        <el-form-item>
          <div class="el-row--flex flex-between">
            <el-button
              type="primary"
              @click="submit"
            >
              确认
            </el-button>
            <el-button
              @click="cancel"
            >
              取消
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {login} from "@/config/api/user";
import {Message} from 'element-ui'
export default {
  name: "login",
  data(){
    return {
      form:{
        account:'',
        password:''
      }
    }
  },
  methods:{
    submit(){
      let _this = this
      this.$axios.$post(login.url,this.form).then(res => {
        if(res.state){
          _this.$store.commit('user/setCurrentUser',res.data)
          window.localStorage.setItem('currentUser',JSON.stringify(res.data))//将登录信息保存到localStorage中
          Message({
            type:'success',
            message:"登录成功",
            onClose(){
              _this.$router.push("/")
            }
          })
        }else{
          Message.error(res.message)
        }
      })
      .catch(error => {
        Message({
          type:'error',
          message:error
        })
      })
    },
    cancel(){

    }
  }
}
</script>

<style lang="less">
.container{
  display: flex;
  justify-content: center;
  align-items: center;
  height: calc(100vh);
  background-image: url("../static/bg.jpeg");
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  .form-container{
    padding: 40px 80px;
    background-color: #fff;
    border-radius: 2px;
    .el-input__inner{
      width: 300px;
    }
    .el-button{
      width: 140px;
    }
  }
}
</style>
